/*
 *  Copyright (c) 2013 Hai Bison
 *
 *  See the file LICENSE at the root directory of this project for copying
 *  permission.
 */

package com.haibison.android.hutieu.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.text.format.DateUtils;

import com.haibison.android.hutieu.ShareContract;
import com.haibison.android.hutieu.ShareProviderUtils;
import com.haibison.android.hutieu.Sys;

/**
 * The global settings.
 * 
 * @author Hai Bison
 * 
 */
public class Settings {

    /**
     * File buffer ({@code 8 KiB}).
     */
    public static final int FILE_BUFFER = 8 * 1024;

    /**
     * The settings file name.
     */
    private static final String SETTINGS_FILENAME = "settings";

    /**
     * This is singleton class.
     */
    private Settings() {
    }// Settings()

    @SuppressLint("InlinedApi")
    private static SharedPreferences p(Context context) {
        return context.getSharedPreferences(Sys.prefix(SETTINGS_FILENAME),
                Context.MODE_MULTI_PROCESS);
    }// p()

    private static final String PKEY_SERVER_SECURITY_MAX_CONCURRENT_THREADS = "server.security.max_concurrent_threads";
    private static final String PKEY_SERVER_MAX_IDLE_TIME = "server.max_idle_time";

    /**
     * Gets server max concurrent threads allowed.
     * 
     * @param context
     *            the context.
     * @return the max concurrent threads allowed, default is {@code 10}.
     */
    public static int getServerMaxConcurrentThreads(Context context) {
        return p(context).getInt(PKEY_SERVER_SECURITY_MAX_CONCURRENT_THREADS,
                10);
    }// getServerMaxConcurrentThreads()

    /**
     * Sets server max concurrent threads allowed.
     * 
     * @param context
     *            the context.
     * @param n
     *            the max concurrent threads allowed
     */
    public static void setServerMaxConcurrentThreads(Context context, int n) {
        p(context).edit()
                .putInt(PKEY_SERVER_SECURITY_MAX_CONCURRENT_THREADS, n)
                .commit();
        context.getContentResolver()
                .notifyChange(
                        ShareProviderUtils.attachPassport(context,
                                ShareContract
                                        .genContentSettingsUriBase(context)),
                        null);
    }// setServerMaxConcurrentThreads()

    /**
     * Gets server max idle time, in milliseconds.
     * 
     * @param context
     *            the context.
     * @return the max idle time, default is 10 minutes.
     */
    public static int getServerMaxIdleTime(Context context) {
        return p(context).getInt(PKEY_SERVER_MAX_IDLE_TIME,
                (int) (DateUtils.MINUTE_IN_MILLIS * 10));
    }// getServerMaxIdleTime()

    /**
     * Sets server max idle time, in milliseconds.
     * 
     * @param context
     *            the context.
     * @param millis
     *            the max idle time.
     */
    public static void setServerMaxIdleTime(Context context, int millis) {
        p(context).edit().putInt(PKEY_SERVER_MAX_IDLE_TIME, millis).commit();
        context.getContentResolver()
                .notifyChange(
                        ShareProviderUtils.attachPassport(context,
                                ShareContract
                                        .genContentSettingsUriBase(context)),
                        null);
    }// setServerMaxIdleTime()

    /**
     * Checks if the app has <b>all</b> {@code permissions} granted.
     * 
     * @param context
     *            the context.
     * @param permissions
     *            list of permissions.
     * @return {@code true} if the app has all permissions asked.
     */
    public static boolean hasPermissions(Context context, String... permissions) {
        for (String p : permissions)
            if (context.checkCallingOrSelfPermission(p) == PackageManager.PERMISSION_DENIED)
                return false;
        return true;
    }// hasPermissions()

}
